﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.U2D.SpriteRendererDataAccessExtensions
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using System.Runtime.CompilerServices;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Bindings;

#nullable disable
namespace UnityEngine.U2D
{
  /// <summary>
  ///   <para>A list of methods that allow the caller to override what the SpriteRenderer renders.</para>
  /// </summary>
  [NativeHeader("Runtime/Graphics/Mesh/SpriteRenderer.h")]
  [NativeHeader("Runtime/2D/Common/SpriteDataAccess.h")]
  public static class SpriteRendererDataAccessExtensions
  {
    internal static unsafe void SetDeformableBuffer(
      this SpriteRenderer spriteRenderer,
      NativeArray<byte> src)
    {
      if ((UnityEngine.Object) spriteRenderer.sprite == (UnityEngine.Object) null)
        throw new ArgumentException(string.Format("spriteRenderer does not have a valid sprite set."));
      if (src.Length != SpriteDataAccessExtensions.GetPrimaryVertexStreamSize(spriteRenderer.sprite))
        throw new InvalidOperationException(string.Format("custom sprite vertex data size must match sprite asset's vertex data size {0} {1}", (object) src.Length, (object) SpriteDataAccessExtensions.GetPrimaryVertexStreamSize(spriteRenderer.sprite)));
      SpriteRendererDataAccessExtensions.SetDeformableBuffer(spriteRenderer, src.GetUnsafeReadOnlyPtr<byte>(), src.Length);
    }

    internal static unsafe void SetDeformableBuffer(
      this SpriteRenderer spriteRenderer,
      NativeArray<Vector3> src)
    {
      if ((UnityEngine.Object) spriteRenderer.sprite == (UnityEngine.Object) null)
        throw new InvalidOperationException("spriteRenderer does not have a valid sprite set.");
      if (src.Length != spriteRenderer.sprite.GetVertexCount())
        throw new InvalidOperationException(string.Format("The src length {0} must match the vertex count of source Sprite {1}.", (object) src.Length, (object) spriteRenderer.sprite.GetVertexCount()));
      SpriteRendererDataAccessExtensions.SetDeformableBuffer(spriteRenderer, src.GetUnsafeReadOnlyPtr<Vector3>(), src.Length);
    }

    internal static unsafe void SetBatchDeformableBufferAndLocalAABBArray(
      SpriteRenderer[] spriteRenderers,
      NativeArray<IntPtr> buffers,
      NativeArray<int> bufferSizes,
      NativeArray<Bounds> bounds)
    {
      int length = spriteRenderers.Length;
      if (length != buffers.Length || length != bufferSizes.Length || length != bounds.Length)
        throw new ArgumentException("Input array sizes are not the same.");
      SpriteRendererDataAccessExtensions.SetBatchDeformableBufferAndLocalAABBArray(spriteRenderers, buffers.GetUnsafeReadOnlyPtr<IntPtr>(), bufferSizes.GetUnsafeReadOnlyPtr<int>(), bounds.GetUnsafeReadOnlyPtr<Bounds>(), length);
    }

    internal static unsafe bool IsUsingDeformableBuffer(
      this SpriteRenderer spriteRenderer,
      IntPtr buffer)
    {
      return SpriteRendererDataAccessExtensions.IsUsingDeformableBuffer(spriteRenderer, (void*) buffer);
    }

    /// <summary>
    ///   <para>Stop using the deformable buffer to render the Sprite and use the original mesh instead.</para>
    /// </summary>
    /// <param name="renderer"></param>
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern void DeactivateDeformableBuffer([NotNull("ArgumentNullException")] this SpriteRenderer renderer);

    internal static void SetLocalAABB([NotNull("ArgumentNullException")] this SpriteRenderer renderer, Bounds aabb)
    {
      SpriteRendererDataAccessExtensions.SetLocalAABB_Injected(renderer, ref aabb);
    }

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern unsafe void SetDeformableBuffer(
      [NotNull("ArgumentNullException")] SpriteRenderer spriteRenderer,
      void* src,
      int count);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern unsafe void SetBatchDeformableBufferAndLocalAABBArray(
      SpriteRenderer[] spriteRenderers,
      void* buffers,
      void* bufferSizes,
      void* bounds,
      int count);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern unsafe bool IsUsingDeformableBuffer(
      [NotNull("ArgumentNullException")] SpriteRenderer spriteRenderer,
      void* buffer);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern void SetLocalAABB_Injected(SpriteRenderer renderer, ref Bounds aabb);
  }
}
